/**
 *****************************************************************************
 * DESC ：. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-8-11 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-8-11		Created.
 ****************************************************************************
 **/
package com.me.client.plugins.TicTacToe;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

/**
 * TicTacToe游戏设置面板. <br/>
 * 下午4:12:51 2012-8-11
 * 
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class TTTSettings extends JFrame {

	private TicTacToe myPlugin = null;
	private JLabel jLabelTTT;
	private JLabel jLabelUserList;
	private JComboBox jComboBoxUsers;
	private JTextArea jTextAreaInfo;
	private JScrollPane jScrollPane;
	private JPanel jPanelWithUser;
	private JLabel jLabelEnemyState;
	private JTabbedPane jTabbedPaneWithUser;
	private JLabel jLabelAIQuality;
	private JComboBox jComboBoxAILevel;
	private JPanel jPanelWithAI;
	private JButton jButtonOK;
	private JButton jButtonCancel;
	private boolean cancelled = false;
	/** 游戏对手,0-对在线用户 1-和电脑. */
	private int gameType = 0;
	/** 选择的在线用户玩家. */
	private String selectedUser = "";
	/** 和电脑玩时游戏的难度级别.0-Hard 1-Normal 2-Stupid */
	private int selectedAI = 0;

	public TTTSettings(TicTacToe plugin) {
		this.myPlugin = plugin;
		this.initComponent();
	}

	/**
	 * 初始化组件.
	 */
	private void initComponent() {
		// TODO Auto-generated method stub
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		jLabelTTT = new JLabel();
		jLabelTTT.setFont(new Font("Dialog", 1, 18));
		jLabelTTT.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelTTT.setIcon(new ImageIcon(this.getClass().getResource("ttticon.png")));
		jLabelTTT.setText("Tic-Tac-Toe");

		jLabelUserList = new JLabel();
		jLabelUserList.setText("Select user:");
		jComboBoxUsers = new JComboBox();
		jTextAreaInfo = new JTextArea();
		jTextAreaInfo.setRows(5);
		jTextAreaInfo.setColumns(20);
		jTextAreaInfo.setEditable(false);
		jTextAreaInfo.setText("Note: you can only play against users which: "
				+ "\n- are online \n- have the TicTacToe plugin installed");
		jTextAreaInfo.setBorder(null);
		jTextAreaInfo.setFocusable(false);
		jTextAreaInfo.setOpaque(false);
		jTextAreaInfo.setRequestFocusEnabled(false);
		jScrollPane = new JScrollPane();
		jScrollPane.setViewportView(jTextAreaInfo);
		jLabelEnemyState = new JLabel();
		jPanelWithUser = new JPanel();
		GroupLayout layoutWithUser = new GroupLayout(jPanelWithUser);
		jPanelWithUser.setLayout(layoutWithUser);
		layoutWithUser.setHorizontalGroup(layoutWithUser.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				GroupLayout.Alignment.TRAILING,
				layoutWithUser
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layoutWithUser
										.createParallelGroup(GroupLayout.Alignment.TRAILING)
										.addComponent(jScrollPane, GroupLayout.Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
										.addComponent(jLabelEnemyState, GroupLayout.Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
										.addGroup(
												layoutWithUser
														.createSequentialGroup()
														.addComponent(jLabelUserList)
														.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 48,
																Short.MAX_VALUE)
														.addComponent(jComboBoxUsers, GroupLayout.PREFERRED_SIZE, 269,
																GroupLayout.PREFERRED_SIZE))).addContainerGap()));
		layoutWithUser.setVerticalGroup(layoutWithUser.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				layoutWithUser
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layoutWithUser
										.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(jComboBoxUsers, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabelUserList))
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jScrollPane, GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabelEnemyState)
						.addContainerGap()));
		jTabbedPaneWithUser = new JTabbedPane();
		jTabbedPaneWithUser.add("Game against real user", jPanelWithUser);
		jLabelAIQuality = new JLabel();
		jLabelAIQuality.setText("AI Quality:");
		jComboBoxAILevel = new JComboBox();
		jComboBoxAILevel.setModel(new DefaultComboBoxModel(new String[]{"Hard", "Normal", "Stupid"}));
		jPanelWithAI = new JPanel();
		GroupLayout layoutWithAI = new GroupLayout(jPanelWithAI);
		jPanelWithAI.setLayout(layoutWithAI);
		layoutWithAI.setHorizontalGroup(layoutWithAI.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				layoutWithAI.createSequentialGroup().addContainerGap().addComponent(jLabelAIQuality)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
						.addComponent(jComboBoxAILevel, GroupLayout.PREFERRED_SIZE, 270, GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));
		layoutWithAI.setVerticalGroup(layoutWithAI.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				layoutWithAI
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layoutWithAI
										.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(jLabelAIQuality)
										.addComponent(jComboBoxAILevel, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addContainerGap(107, Short.MAX_VALUE)));
		jTabbedPaneWithUser.add("Game against computer (AI)", jPanelWithAI);
		jButtonOK = new JButton();
		jButtonOK.setText("OK - Start game");
		jButtonOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				startGame();
			}
		});
		jButtonCancel = new JButton();
		jButtonCancel.setText("Cancel");
		jButtonCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cancelSetting();
			}
		});
		GroupLayout layout = new GroupLayout(this.getContentPane());
		this.getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
										.addComponent(jTabbedPaneWithUser, GroupLayout.Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
										.addComponent(jLabelTTT, GroupLayout.Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
										.addGroup(
												layout.createSequentialGroup().addComponent(jButtonOK)
														.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
														.addComponent(jButtonCancel))).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jLabelTTT, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jTabbedPaneWithUser, GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(
								layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jButtonCancel)
										.addComponent(jButtonOK)).addContainerGap()));
		this.pack();
	}

	/**
	 * 设置在线用户列表.
	 * 
	 * @param onlineUserList
	 *            在线用户.
	 */
	public void setUserList(List<String> onlineUserList) {
		System.out.println("Setting List.: " + onlineUserList.toString());
		this.jComboBoxUsers.setModel(new DefaultComboBoxModel(onlineUserList.toArray()));
	}
	
	/**
	 * 取消设置.
	 */
	private void cancelSetting(){
		this.cancelled  = true;
		this.setVisible(false);
	}
	
	/**
	 * 开始游戏.
	 */
	private void startGame(){
		this.cancelled = false;
		this.gameType  = this.jTabbedPaneWithUser.getSelectedIndex();
		this.selectedUser  = (String)this.jComboBoxUsers.getSelectedItem();
		this.selectedAI  = this.jComboBoxAILevel.getSelectedIndex();
		this.myPlugin.startNewGame(false);
		this.setVisible(false);
	}

	/**
	 * 获取游戏玩家类型,AI或在线用户.
	 * @return
	 */
	public int getGameType() {
		return this.gameType;
	}

	/**
	 * 获取所选择的在线用户.
	 * @return
	 */
	public String getSelectedUser() {
		return this.selectedUser;
	}

	/**
	 * 获取AI的级别,Hard Normal Stupid.
	 * @return
	 */
	public int getSelectedAI() {
		return this.selectedAI;
	}

}
